
import {reqLogin} from '../api/index'
import storageUtils from '../utils/storageUtils'
import {RECEIVE_USER,SHOW_ERROR_MSG,RESET_USER,SET_HEAD_TITLE} from './action_types'

/**设置头部标题的action */
// export const setHeaderTitle = (headertitle)=>({type:SET_HEADER_TITLE,headertitle})
export const setHeadTitle = (headTitle) => ({type: SET_HEAD_TITLE, data: headTitle})


/*
接收用户的同步action
 */
export const receiveUser  = (user)=>({type:RECEIVE_USER,user})

/*
显示错误信息的同步action
 */
export const showErrorMsg = (errorMsg) => ({type: SHOW_ERROR_MSG, errorMsg})        

/*
登陆的异步action
 */
export const login = function (username,password){
    return async dispatch =>{
        const result = await reqLogin(username,password)

        if(result.status === 0){
            const user = result.data
            storageUtils.saveUser(user)
            dispatch(receiveUser(user))
        }else{
            const msg = result.msg
            dispatch(showErrorMsg(msg))
        }
    }
}
// 退出的action
export const logout = ()=>{
    storageUtils.removeUser()
    return {type:RESET_USER}
}